package com.pwl.shiro.realm;

import org.apache.shiro.authc.AuthenticationException;
import org.apache.shiro.authc.AuthenticationInfo;
import org.apache.shiro.authc.AuthenticationToken;
import org.apache.shiro.authc.SimpleAuthenticationInfo;
import org.apache.shiro.authz.AuthorizationInfo;
import org.apache.shiro.realm.AuthorizingRealm;
import org.apache.shiro.subject.PrincipalCollection;
import org.apache.shiro.util.ByteSource;

public class CustomRealm extends AuthorizingRealm {

    @Override
    public String getName() {
        return super.getName();
    }

    //授权器
    @Override
    protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principalCollection) {
        return null;
    }

    //认证器
    @Override
    protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken token) throws AuthenticationException {
        //模仿数据库获取  账号 密码
        String jdbcUsername="zhangsan";
        String jdbcPassword="1ab860d4a40247fc2929cf4b7d7f3241";
        String  salt="fsafw";
        if (!jdbcUsername.equals(token.getPrincipal().toString())){
            return null;
        }
        return new SimpleAuthenticationInfo(token.getPrincipal(),jdbcPassword, ByteSource.Util.bytes(salt),getName());
    }
}
